package yxy.game.pm2.active.command.holiday;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.atcholidayshop;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_节日活动;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.module.CurrencyHandle;

/**
 * Author HYB
 * 节日_兑换
 */
@MessageType(proto = Command.ActHolidayExchageRequest, classtype = ActiveMessage.ActHolidayExchageRequest.class)
public class CMD_Active_节日_Exchange extends Command<ActiveMessage.ActHolidayExchageRequest> {

    @Override
    public void execute(MessageSource messageSource, ActiveMessage.ActHolidayExchageRequest request) throws Exception {
        String pcode = messageSource.getTag();
        ActiveMessage.ActHolidayExchageResponse.Builder response = ActiveMessage.ActHolidayExchageResponse.newBuilder();
        int id = request.getId();
        int num = request.getNum();
        if (num < 1) {
            send(messageSource, response, Result.ParamError);
            return;
        }
        ActiveModule_节日活动 am = ActiveModule_节日活动.get(pcode,false);
        if (am == null) {
            send(messageSource, response, Result.ActNoOpen);
            return;
        }
        DrawStatus cangainreward = am.canGainExchageReward(id, num);
        if (cangainreward == DrawStatus.hadget) {
            send(messageSource, response, Result.ActHadGet);
            return;
        }
        if (cangainreward == DrawStatus.cannotget) {
            send(messageSource, response, Result.ActCannotGet);
            return;
        }
        //扣道具
        atcholidayshop data = DataCenter.getData(id, atcholidayshop.class);
        if (data == null) {
            send(messageSource, response, Result.ActCannotGet);
            return;
        }
        Currency costcur = Currency.create(data.getCosttype(), data.getCostid(), data.getCostnum() * num);
        try {
            new CurrencyHandle().cost(pcode, CCReason.活动_节日_兑换, costcur);
        } catch (CurrencyNotEnoughException e) {
            send(messageSource, response, new Result(Result.CostFail, costcur.getType().getNumber(), costcur.getDataId()));
            return;
        }
        am.addExchangeNum(id, num);
        //发奖励
        Currency cur = Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum() * num);
        if (cur == null) {
            send(messageSource, response, Result.DataError);
            return;
        }
        response.addDisplay(new CurrencyHandle().packItem(cur));
        //下发道具
        new CurrencyHandle().addCurrency(pcode, CCReason.活动_节日_兑换, cur);


        send(messageSource, response, Result.Success);

    }

    private void send(MessageSource messageSource, ActiveMessage.ActHolidayExchageResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }

}
